#!/bin/sh
# ecmctl - Control Script for Start/Stop ECM System
# NOTE: this script should be called in ecmctl environment
#       (ie. call by some process which is run by ecmctl
# Copyright (c) 2001-2006 by Clunix Inc. All Rights Reserved.
#
# chkconfig: 345 90 10
# description: ECM is a set of cluster management daemons
# usage: ecmctl {start|stop|restart|status}

if [ -z "$CLX_HOME" ]; then
    exepath=$(cd ${0%/*} && echo $PWD/${0##*/})
    cd `dirname $exepath`
    cd ..
    CLX_HOME=`pwd`
else
    cd $CLX_HOME
fi

. $CLX_HOME/bin/ecmfunctions

ECONF_ARGS=
my_name=$0


while getopts hp:e:b:c:n:C c; do
    case $c in
	h) help=yes
	;;
	p) ECONF_ARGS="$ECONF_ARGS -p $OPTARG"
	;;
	e) ECONF_ARGS="$ECONF_ARGS -e $OPTARG"
	;;
	b) ECONF_ARGS="$ECONF_ARGS -b $OPTARG"
	;;
	c) CLNAME=$OPTARG
	;;
	n) NDNAME=$OPTARG
	;;
	C) CLEAR_CONF=-C
	;;
	*) argerror=yes
	;;
    esac
done

shift `expr $OPTIND - 1`


# Help Message -------------------------------------------------

function usage() {
    cat <<EOF
Usage: $myname [-hpecnC] {start|stop|restart|status} [ebcd] [edbd] [egid]"
options:
  -h: help
  -p: ebc port (default 908)
  -e: egid port (default 80)
  -c: cluster name (default ECMCluster)
  -n: node name
  -C: clear edbd snapshot
target daemon names:
  if target daemon name is not specified, controls all daemon by default 
EOF
}

if [ -n "$help" ]; then
    usage
    exit 0
fi
if [ -n "$argerror" ]; then
    usage
    exit 1
fi

# Setup Environments ------------------------------------------

EBCD_ARGS=${EBCD_ARGS:="-D"}
EDBD_ARGS=${EDBD_ARGS:="-D -v -l log/edbd.log $CLEAR_CONF"}
EGID_ARGS=${EGID_ARGS:="-D"}

# Setting path to be used all ecm processes
PATH=$CLX_HOME/bin:/usr/sbin:/sbin:/bin:/usr/bin:$PATH

# Export variables
export CLX_HOME EBC_PORT

# Check econf sanity

if [ ! -e etc/econf ]; then
    if [ -z "$NDNAME" ]; then
	NDNAME=`hostname`
    fi
    if [ -z "$CLNAME" ]; then
	CLNAME=ECMCluster
    fi
fi
if [ ! -z "$NDNAME" ]; then
    ECONF_ARGS="$ECONF_ARGS -n $NDNAME"
fi
if [ ! -z "$CLNAME" ]; then
    ECONF_ARGS="$ECONF_ARGS -c $CLNAME"
fi

if [ ! -z "$ECONF_ARGS" ]; then
    bin/ecmconf $ECONF_ARGS
fi

if [ ! bin/ecmconf ]; then
    cat <<EOF

ECM Configuration Error
You should specify cluster name and/or node name. See help..

EOF
    exit 2
fi

# Base checking scripts ----------------------------------------------------

# If you want get core on daemon failure, uncomment this
# ulimit -c unlimited


# Control Commands --------------------------------------------
# Current directory is $CLX_HOME

ACTION=$1
shift

# get target daemon list
DO_EBCD=
DO_EDBD=
DO_EGID=
while [ -n "$1" ]; do
    case $1 in
    ebcd) DO_EBCD=yes ;;
    edbd) DO_EDBD=yes ;;
    egid) DO_EGID=yes ;;
    *) echo "unknown daemon name; ignore: $1" ;;
    esac 
    shift
done
if [ -z "$DO_EBCD$DO_EDBD$DO_EGID" ]; then
    DO_EBCD=yes
    DO_EDBD=yes
    DO_EGID=yes
fi

case $ACTION in
start)
    if [ -n "$DO_EBCD" ]; then
        start_daemon ebcd $EBCD_ARGS || exit 1
    fi
    if [ -n "$DO_EDBD" ]; then
        start_daemon edbd $EDBD_ARGS
        case $? in
	0) ;;
	2) echo invalid or expired license key; exit 2 ;;
	*) exit 2 ;;
        esac
    fi
    if [ -n "$DO_EGID" ]; then
    	start_daemon egid $EGID_ARGS || exit 2
    fi
    ;;

stop)
    if [ -n "$DO_EGID" ]; then stop_daemon egid; fi
    if [ -n "$DO_EDBD" ]; then stop_daemon edbd; fi
    if [ -n "$DO_EBCD" ]; then stop_daemon ebcd; fi
    # stop always returns success
    ;;

restart)
    # stop
    if [ -n "$DO_EGID" ]; then stop_daemon egid; fi
    if [ -n "$DO_EDBD" ]; then stop_daemon edbd; fi
    if [ -n "$DO_EBCD" ]; then stop_daemon ebcd; fi
    # start
    if [ -n "$DO_EBCD" ]; then
        start_daemon ebcd $EBCD_ARGS || exit 1
    fi
    if [ -n "$DO_EDBD" ]; then
        start_daemon edbd $EDBD_ARGS
        case $? in
	    0) ;;
	    2) echo invalid or expired license key; exit 2 ;;
	    *) exit 2 ;;
        esac
    fi
    if [ -n "$DO_EGID" ]; then
    	start_daemon egid $EGID_ARGS || exit 2
    fi
    ;;

status)
    if [ -n "$DO_EBCD" ]; then status_daemon ebcd; fi
    if [ -n "$DO_EDBD" ]; then status_daemon edbd; fi
    if [ -n "$DO_EGID" ]; then status_daemon egid; fi
    ;;

*)
    usage
    exit 1
esac

exit 0

